/*
THIS FILE COMPUTES THE RISK MEASURE DEFINED IN THE PAPER EQUATION 26
*/

***********************************************************************
*** START WITH AVERAGE COUNTRY SHARES
* merge with average country share
use "$datapath/Portfolio_Paper_zbar.dta", clear
* reduce dimensions
collapse zbar_* , by(fundid country)

compress
* save data in memory
tempfile master
save `master', replace
***********************************************************************

***********************************************************************
* RISK MEASURE FOR EACH FUND
local j = 1
qui levelsof fundid, local(fundid)
foreach f of local fundid { // for each fund
	dis "." _continue
	if mod(`j',10) == 0 {
			dis " `j' " _continue        
	}
	quietly {
		use `master', clear

		keep if fundid == `f'
		merge 1:m country using "$datapath/MSCI_daily_equity_returns", keep(3) nogenerate // daily equity returns
		
		***************************************************************************
		*** reference return: weighted average other countries in which fund invests
		levelsof country, local(country)
		foreach c of local country {
			*** main
			qui gen s_ref_`c' = zbar_`c'*r
		}
		***************************************************************************

		***************************************************************************
		* reduce information and reshape the data
		qui collapse (sum) s_* , by(fundid dated)

		foreach v of varlist s_* {
			qui replace `v' = . if `v' == 0
		}

		qui reshape long s_ref_ , i(dated fundid) j(country) string // fund-country-day dimension
		drop if mi(s_ref_)
		rename s_ref_ ref
		***************************************************************************

		***************************************************************************
		* import us differentials
		merge 1:1 country dated using "$datapath/MSCI_daily_equity_returns", nogenerate
		drop if mi(fundid)
		***************************************************************************

		* excess return
		gen er = r - ref
		merge m:1 country fundid using "$datapath/Funds_zbarCollapse.dta", keep(3) nogenerate // average z for fund f in the countries 

		gen rhs = zbar * r + (1-zbar)*ref
		
		***************************************************************************
		*** COVARIANCE
		* nice date format
		gen datem = mofd(dated)
		format datem %tm
		* covariance
		bys country datem: egen risk = corr(er rhs), cov
		* reduce dimensions
		cap collapse risk, by(fundid country datem)
		bys fundid country (datem): gen frisk = risk[_n+1]
		bys fundid country (datem): gen lrisk = risk[_n-1]
		cap keep datem fundid country risk frisk lrisk
		cap tempfile temp_`f'
		cap save `temp_`f'', replace
		
		local j = `j' + 1
	}
	
}
***************************************************************************

***************************************************************************
* MERGE FUND RISK MEASURES
use `temp_333', clear
foreach f of local fundid {
	if `f' > 333 {
		cap merge 1:1 fundid country datem using `temp_`f'', nogenerate
	}
}

keep fundid country datem *risk 
sort fundid country datem

save "$datapath/Funds_Risk.dta", replace
* end
***************************************************************************